<?php
require DZ_ROOT . 'model/model_forum_post.php';
require DZ_ROOT . 'model/model_forum_thread.php';
require_once DZ_ROOT . 'table/table_forum_thread.php';

class DiscuzThreadDAO {
	
	public $threadDao = array(); 

	private $prefix = CLOSE_PREFIX;

    function __construct() {
    	msg("Init DiscuzThreadDAO");
    	$this->threadDao = new table_forum_thread();
    	if(ISGBK) {
    		$this->prefix = iconv("UTF-8", "GB2312//IGNORE", $this->prefix);
    	}
    }

    public function reply($user, $message) {
    	msg("Reply question for " . $user['username']);
    	msg("Content is " . $message['Content']);
    	$post = new model_forum_post($user['tid']);
    	// Convert to gbk
    	if(ISGBK) {
    		$message['Content'] = iconv("UTF-8", "GB2312//IGNORE", $message['Content']);
    		$message['subject'] = iconv("UTF-8", "GB2312//IGNORE", $message['subject']);
    		$user['username'] = iconv("UTF-8", "GB2312//IGNORE", $user['username']);
    	}
    	$post->member = $user;
    	$parameters = array(
    			"subject" => $message['subject'],
    			"message" => $message['Content'],
    			"special" => 0,
    			"publishdate" => time()
    	);
    	$post->newreply($parameters);
    	msg("Complete reply question ");
    }
    
    public function newQuestion($user, $message) {
    	msg("Create a new question for " . $user['username']);
    	msg("Content is " . $message['Content']);
    	$thread = new model_forum_thread($user['fid']);
    	

    	$message['Content'] = $message['Content'] .
    	"\n\n\n\n论坛只支持文字回答；老师也可以用微信回答（支持语音加图片）。发送问题id（见标题）到服务号即可开始回答该问题。";
    	
    	// Convert to gbk
    	if(ISGBK) {
    	    $message['Content'] = iconv("UTF-8", "GB2312//IGNORE", $message['Content']); 
    		$message['subject'] = iconv("UTF-8", "GB2312//IGNORE", $message['subject']);
    		$user['username'] = iconv("UTF-8", "GB2312//IGNORE", $user['username']); 
    	}    	
    	$parameters = array(
    		"subject" => $message['subject'],
    		"message" => $message['Content'],
    		"special" => 0,
    		"publishdate" => time()
    	);
    	$thread->member = $user;
    	$thread->newthread($parameters);
    	msg("Complete create question ");
        $thread_result = $this->threadDao->fetch($thread->tid);
        $number = "问题 $thread->tid: ";
        // Convert to gbk
        if(ISGBK) {
        	$number = iconv("UTF-8", "GB2312//IGNORE", $number);
        }
    	$thread_result['subject'] = $number . $thread_result['subject'];
    	$this->threadDao->update($thread->tid, $thread_result);
    	msg("Title updated");
    	return $thread->tid;
    }
    
    public function closeQuestion($user) {
    	msg("Close question for " . $user['username']);
    	$thread = $this->threadDao->fetch($user['tid']);
    	$thread['subject'] = $this->prefix . $thread['subject'];
    	$this->threadDao->update($user['tid'], $thread);
    	msg("Close question success ");
    }
    
    public function findQuestionById($id) {
    	msg("Find question by " . $id);
    	return $this->threadDao->fetch($id);
    }
    
    public function isClosed($thread) { 	
    	return preg_match("/^$this->prefix/", $thread['subject']);
    }
}

?>
